My PNG has transparency, but after saving with PHP GD, transparency is lost [closed]

Posted by Harry Stroker on Stack Overflow See other posts from Stack Overflow or by Harry Stroker
Published on 2011-11-13T01:50:59Z Indexed on 2011/11/13 9:51 UTC
Read the original article Hit count: 462

Filed under:
|
|
|
|

I found the solution to my problem. See below the original post and completely at the bottom my solution. I made a stupid mistake :)


First I crop an image and then save it to a png file. Right after this, I also show the image.
However, the saved png does not have transparency and the shown one has. What is going on?

$this->resource = imagecreatefrompng($this->url);

imagealphablending($this->resource, false);
imagesavealpha($this->resource, true);

$newResource = imagecreatetruecolor($destWidth, $destHeight);
imagealphablending($newResource, false);
imagesavealpha($newResource, true);

$resample = imagecopyresampled($newResource,$this->resource,0,0,$srcX1,$srcY1,$destWidth,$destHeight,$srcX2-$srcX1, $srcY2-$srcY1);
imagedestroy($this->resource);
$this->resource = $newResource;

// SAVING
imagepng($this->resource, $destination, 100);

// SHOWING
header('Content-type: image/png');
imagepng($this->resource);


The reason I also save the image is for caching. If the script is executed on a png, it saves a cached png.
Next time the image is requested, the png file will be shown, but it has lost its transparency.

Even stranger:

  • When I save that cached png image as (within Firefox), it saves it suddenly as a jpg, even though the extension was png.
  • Downloading the cached png using chrome and opening it in Photoshop gives the error: "file-format module cannot parse the file".

I will show you the shown PNG and the generated PNG: http://www.foodmuseum.nl/SaveProblemTransparency.png

Once I try to show that saved PNG with the GD library, it gives me an error.


EDIT

NO NO NO NO THIS IS NOT A DUPLICATE!!!... I ALREADY USED THEIR SOLUTION. The solution in the supposedly duplicate works for showing my image. But I also try to save it with the exact same resource, but then it has no transparency.


EDIT 2 - SOLUTION

I found out what the problem was. It was a stupid mistake. The script I provided above were cut out of a class and placed as sequential code, while in real this is not what exactly happened. The save image function:

function saveImage($destination,$quality = 90)
{
    $this->loadResource();
    switch($extension){
        default:
        case 'JPG': 
        case 'jpg': 
            imagejpeg($this->resource, $destination, $quality);
            break;
        case 'gif': 
            imagegif($this->resource, $destination);
            break;
        case 'png': 
            imagepng($this->resource, $destination);
            break;
        case 'gd2': 
            imagegd2($this->resource, $destination);
            break;
    }
}

However... $extension does not exist. I fixed it by adding:

$extension = $this->getExtension($destination);

© Stack Overflow or respective owner

Related posts about php

Related posts about image